package com.unicom.vw.modules.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.unicom.vw.common.utils.JsonDateValueProcessor;
import com.unicom.vw.common.utils.R;
import com.unicom.vw.modules.sys.dao.DownloadAddressDao;
import com.unicom.vw.modules.sys.dao.GetversionsDao;
import com.unicom.vw.modules.sys.entity.DownloadAddressEntity;
import com.unicom.vw.modules.sys.entity.GetversionsEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@RestController
@RequestMapping("/app")
public class AppGetVersionsController {
	
	@Autowired
	private GetversionsDao getversionsDao;
	
	@Autowired
	private DownloadAddressDao downloadAddressDao;
	
	@RequestMapping("/getVersions")
	public Map<String, Object> getVersion(String appVersion,String os){
		try {
			String versionrp = appVersion.substring(appVersion.lastIndexOf(".")+1,16);
			List<GetversionsEntity> appList  = getversionsDao.getVersion(versionrp, os);
			List<DownloadAddressEntity> addressList  = downloadAddressDao.getAddress(os);
			JsonConfig jsonConfig = new JsonConfig();  
			jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());  
			Map<String, Object> map = new HashMap<>();
			map.put("data", appList);
			JSONObject stationjson = JSONObject.fromObject(map,jsonConfig);
			JSONArray stationarray = stationjson.getJSONArray("data");
			for (int k = 0; k < stationarray.size(); k++) {
				JSONObject station = stationarray.getJSONObject(k);
				station.put("url", addressList);
			}
			return R.ok(stationjson);
		} catch (Exception e) {
			// TODO: handle exception
			return R.error(1, "版本号不正确");
		}
		
	}
}
